# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +---------------------------------------------------------------------
import time
from tortoise.expressions import Q
from hypertext import PagingResult
from common.utils.urls import UrlUtil
from common.models.music import MusicStylesModel
from apps.admin.schemas.apply.music import styles_schema as schema


class MusicStylesService:

    @classmethod
    async def lists(cls, params: schema.MusicStylesSearchIn) -> PagingResult[schema.MusicStylesVo]:
        """
        音乐风格列表

        Args:
            params (schema.MusicStylesSearchIn): 搜索参数

        Returns:
            PagingResult[schema.MusicStylesVo]

        Author:
            zero
        """
        # 查询条件
        where = MusicStylesModel.build_search({
            "=": ["is_disable"],
            "%like%": ["name"],
            "datetime": ["start_time|end_time@create_time"]
        }, params.__dict__)

        where.append(Q(is_delete=0))

        # 查询数据
        _model = MusicStylesModel.filter(*where).order_by(str("-id"))
        _pager = await MusicStylesModel.paginate(
            model=_model,
            page_no=params.page_no,
            page_size=params.page_size
        )

        _lists = []
        for item in _pager.lists:
            _lists.append(schema.MusicStylesVo(
                id=item["id"],
                name=item["name"],
                value=item["value"],
                sort=item["sort"],
                image=await UrlUtil.to_absolute_url(item["image"]),
                is_disable=item["is_disable"],
                create_time=item["create_time"]
            ))

        _pager.lists = _lists
        return _pager

    @classmethod
    async def add(cls, post: schema.MusicStylesAddIn):
        """
        音乐风格新增

        Args:
            post (schema.MusicStylesEditIn): 参数

        Author:
            zero
        """
        await MusicStylesModel.create(
            image=UrlUtil.to_relative_url(post.image),
            name=post.name,
            value=post.value,
            sort=post.sort,
            is_disable=post.is_disable,
            create_time=int(time.time()),
            update_time=int(time.time())
        )

    @classmethod
    async def edit(cls, post: schema.MusicStylesEditIn) -> None:
        """
        音乐风格编辑

        Args:
            post (schema.MusicStylesEditIn): 参数

        Author:
            zero
        """
        await MusicStylesModel.filter(id=post.id, is_delete=0).get()
        await MusicStylesModel.filter(id=post.id).update(
            image=UrlUtil.to_relative_url(post.image),
            name=post.name,
            value=post.value,
            sort=post.sort,
            is_disable=post.is_disable,
            update_time=int(time.time())
        )

    @classmethod
    async def delete(cls, id_: int) -> None:
        """
        音乐风格删除

        Args:
            id_ (int): ID

        Author:
            zero
        """
        await MusicStylesModel.filter(id=id_, is_delete=0).get()
        await MusicStylesModel.filter(id=id_).update(
            is_delete=1,
            delete_time=int(time.time())
        )

